# -*- coding: utf-8 -*-

"""
@author: Laowang
@contact: QQ:1125564921
@Created on: 2022/7/7
@Remark: 
"""
import os

from abc import ABCMeta
from typing import Dict, List
from functools import lru_cache


class PlatConfig(metaclass=ABCMeta):
    """
    FRAMEWORK_STRATEGY:

        value:
         * pro
         * test
         * dev
        default:
         * Gets the last one in the configuration information as the default value

    """

    def __new__(cls, *args, **kwargs) -> object:

        env = os.environ.get("FRAMEWORK_STRATEGY", None)
        domain = list(
            kwargs.values())[-1] if not env else kwargs.get(env, "test")
        return domain


class DomainAPIField(PlatConfig):

    """
    Domain name environment to distinguish different domain name problems in different environments:  
    Domain name environments are divided into two categories:  

    -1- Test environment  
    -2- Production environment 

        kwargs:
        * test
        * dev
        * pro
    """

    def __init__(self, **kwargs) -> None:

        assert any([
            "pro" not in kwargs,
            "test" not in kwargs,
            "dev" not in kwargs,
        ])


@lru_cache(maxsize=1000000000)
def overloadcls(cls):
    """
    Case study:  
    This parameter is used to distinguish domain names configured in different environments  
    Parameter information is divided into Domain whose value must be a dictionary type  
    ---  
    The value of the other child routes can be set to a string 

    Example:
        class TestAPIConfig:

            Domain = {
                "pro" : "xxxx.com",
                "test" : "xxx.com",
                "dev" : "123.com",
            }

            OHTER_API_1 = "/xx/xx/xxx"
            OHTER_API_2 = "/xx/xx/xxx"
            OHTER_API_3 = "/xx/xx/xxx"

    Usage:
        TestAPIConfig.OHTER_API_1 == xxxx.com/xx/xx/xxx

    The environment selected is based on the Domain configuration and the value of the environment variable FRAMEWORK_STRATEGY 
    """
    __domain = ""
    for __name, __value in vars(cls).items():
        if __name.startswith("_"):
            continue
        if isinstance(__value, Dict):
            __domain = DomainAPIField(**__value)
            setattr(cls, __name, __domain)
            continue
        setattr(cls, __name, __domain + __value)
    return cls
